import Vue from 'vue'
import Vuex from 'vuex'
import {getTimeInterval} from '../utils/index'
import user from './modules/user'
Vue.use(Vuex)
// 略:后台获取系统运行时间
const runAt = '1589878800000';
let timer = null;
const state = {
    loading: false,
    runTimeInterval: '',
    socials: '',
    websiteInfo: ''
}
const mutations = {
    SET_LOADING: (state, v) => {
        state.loading = v;
    },
    SET_SOCIALS: (state, v) => {
        state.socials = v;
    },
    SET_SITE_INFO: (state, v) => {
        state.websiteInfo = v;
    },
    GET_RUNTIME_INTERVAL: (state) => {
        if (!timer || !state.runTimeInterval) {
            clearInterval(timer)
            timer = setInterval(() => {
                state.runTimeInterval = getTimeInterval(runAt);
            }, 1000);
        }
    }
}
const actions = {
    setLoading: ({commit}, v) => {
        commit('SET_LOADING', v);
    },
    // 获取站点信息
    getSiteInfo: ({commit, state}) => {
        return new Promise(resolve => {
            if (state.websiteInfo) {
                resolve(state.websiteInfo)
            } else {
                let data = {
                    avatar: 'https://s2.ax1x.com/2020/01/17/1SCadg.png',
                    slogan: 'The way up is not crowded, and most chose ease.',
                    name: 'FZY′blog',
                    domain: 'https://www.fengziy.cn',
                    notice: '本博客的Demo数据由Mockjs生成',
                    desc: '一个It技术的探索者'
                }
                commit('SET_SITE_INFO', data);
                resolve(data);
            }
        })
    },
    // 站点社交信息
    getSocials: ({commit, state}) => {
        return new Promise((resolve => {
            if (state.socials) {
                resolve(state.socials)
            } else {
                let data = [{
                    id: 1,
                    title: 'Akina',
                    img: 'https://s1.ax1x.com/2020/05/14/YDfRnU.jpg'
                },
                    {
                        id: 2,
                        title: '使用说明',
                        img: 'https://s1.ax1x.com/2020/05/14/YDf4AJ.jpg'
                    },
                    {
                        id: 3,
                        title: '文章归档',
                        img: 'https://s1.ax1x.com/2020/05/14/YDfT91.jpg'
                    }]
                commit('SET_SOCIALS', data);
                resolve(data);
            }
        }))
    }
}
const getters = {
    loading: state => state.loading,
    runTimeInterval: state => state.runTimeInterval,
    notice: state => state.websiteInfo ? state.websiteInfo.notice : ''
}
export default new Vuex.Store({
    state,
    mutations,
    actions,
    modules: {
        user
    },
    getters
})
